package com.kgc.ebook.ebookmanager.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.ebook.ebookmanager.pojo.Ebookentry;
import com.kgc.ebook.ebookmanager.pojo.ResultEntity;
import com.kgc.ebook.ebookmanager.service.EbookentrySrevice;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EntryController {
    @Resource
    EbookentrySrevice ebookentrySrevice;
    /*查询分页*/
    @RequestMapping("/selectEntry")
    public ResultEntity <PageInfo<Ebookentry>> seleEntry(
            @RequestParam(value = "categoryId",required = false,defaultValue = "0") Integer categoryId,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "2") Integer pageSize
    ){
PageInfo<Ebookentry> pageInfo=ebookentrySrevice.select(pageNum,pageSize,categoryId);
return  ResultEntity.successWithoutData(pageInfo);

    }


    @RequestMapping("/saveEntry")
    public  ResultEntity<Integer> saveEntry(@RequestBody Ebookentry entry){
        Integer add=0;
        if (entry.getId()==null){
            /*添加*/
            add = ebookentrySrevice.add(entry);
        }else {
            //修改
            add = ebookentrySrevice.update(entry);
        }

        return  ResultEntity.successWithoutData(add);
    }

    /*根据id查询*/
    @RequestMapping("/getEntryById")
    public  ResultEntity<Ebookentry>  getByid(Integer id){
        Ebookentry ebookentry = ebookentrySrevice.selectByid(id);
        return  ResultEntity.successWithoutData(ebookentry);
    }
    /*根据id删除*/
    @RequestMapping("deleEntryByid")
    public ResultEntity<Integer> delerod(Integer id){
Integer integer=ebookentrySrevice.delete(id);
return  ResultEntity.successWithoutData(integer);

    }

}
